package pl.minim.unidefCMS.objects {
	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class UnidefClass extends AbstractUnidefObject {

		private var objects:Array = [];		public var descriptions:Dictionary = new Dictionary();
		public var clazzID:String;
		private var rawData:Object;
		public var className:String;

		public function UnidefClass(clazzID:String, className:String) {
			this.className = className;
			this.clazzID = clazzID;
		}

		public function addObject(unidef:AbstractUnidefObject, desc:String=null):void {
			objects.push(unidef);			descriptions[unidef]=desc;
			unidef.parent = this;
		}

		public function getObjects():Array {
			return objects.concat();
		}

		override public function toXML():XML {
			var xml:XML = <i/>;
			xml.@id = id;
			var i:uint;
			var loop:AbstractUnidefObject;
			for(i = 0;i < objects.length;i++) {
				loop = objects[i];
				xml.appendChild(loop.toXML());
			}
			return xml;
		}

		override public function getTypeName():String {
			return 'Class(' + className+')';
		}

		override public function getDataAsTextPreview():String {
			return '';
		}

		override public function reactToPredefinedObjectDelete(deletedObject:UnidefClass):void {			
			var i:uint;
			var loop:AbstractUnidefObject;
			for(i = 0;i < objects.length;i++) {
				loop = objects[i];
				loop.reactToPredefinedObjectDelete(deletedObject);
			}
		}

		override public function exportToRawData():* {
			if(!rawData) {
				var obj:Object = {};
				var i:uint;
				var loop:AbstractUnidefObject;
				for(i = 0;i < objects.length;i++) {
					loop = objects[i];
					obj[loop.id] = loop.exportToRawData();
				}
				rawData = obj;
			}
			return rawData;
		}
	}
}
